<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class biaya extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_biaya');
	}
	
	function index(){
		$this->load->view('master/biaya');
	}
	
	function grid(){
		$data = $this->mdl_biaya->getdata();
		echo $this->mdl_biaya->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['kode'] = '';
		$data['kd_prodi'] = '';
		$data['kd_fakultas'] = '';
		$data['nama_skema'] = '';
		$data['ukt'] = '';
		$data['ukv'] = '';
		$data['biaya_pengembangan'] = '';
		$this->load->view('master/biaya_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_biaya->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['nama_skema'] = $r->row()->nama_skema;
		$data['ukt'] = $r->row()->ukt;
		$data['ukv'] = $r->row()->ukv;
		$data['biaya_pengembangan'] = $r->row()->biaya_pengembangan;
		$this->load->view('master/biaya_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("kd_fakultas", 'Fakultas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Prodi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_skema", 'Nama Skema', 'trim|required|xss_clean');
		$this->form_validation->set_rules("ukt", 'UKT', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules("ukv", 'UKV', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules("biaya_pengembangan", 'Biaya Pengembangan', 'trim|required|numeric|xss_clean');
		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_biaya->InsertOnDb($data);
			}else { // edit
				$result=$this->mdl_biaya->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_biaya->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}